package com.fuchenyang.getway.predicates;


import com.fuchenyang.getway.bean.Age;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;


import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;


@Component
public class AgeRoutePredicateFactory extends AbstractRoutePredicateFactory<Age> {
    public AgeRoutePredicateFactory() {
        super(Age.class);
    }

    // 依次设置AgeConfig参数和yml文件中的值对应
    public List<String> shortcutFieldOrder() {
        // 这里将配置文件中的值按照顺序赋值
        return Arrays.asList("min", "max");
    }

    @Override
    public Predicate<ServerWebExchange> apply(Age config) {
        // 接收到用户传递的参数
        return exchange -> {
            String a = exchange.getRequest().getQueryParams().get("age").get(0);
            int age = Integer.parseInt(a);
            return age > config.getMin() & age < config.getMax();
        };
    }
}
